package com.tsinghuabigdata.edu.ddmath.module.onlineTutoring;

import com.tsinghuabigdata.edu.ddmath.MVPModel.RequestListener;
import com.tsinghuabigdata.edu.ddmath.bean.ClassInfo;
import com.tsinghuabigdata.edu.ddmath.bean.MyCourse;
import com.tsinghuabigdata.edu.ddmath.commons.http.AppAsyncTask;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.ApplyTutor;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.OnlineTutoring;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorApplied;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorQueue;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorState;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.TutorTurn;
import com.tsinghuabigdata.edu.ddmath.module.onlineTutoring.bean.UploadImage;
import com.tsinghuabigdata.edu.ddmath.requestHandler.MyLearnService;
import com.tsinghuabigdata.edu.ddmath.requestHandler.TutorService;
import com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl.MyLearnServiceImpl;
import com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl.TutorServiceImpl;

import java.util.List;

/**
 * Created by Administrator on 2017/3/11.
 */

public class TutorModel {

    private TutorService mTutorService = new TutorServiceImpl();

    /**
     * 获取老师辅导状态
     */
    class TeacherStateTask extends AppAsyncTask<String, Void, List<OnlineTutoring>> {

        private RequestListener reqListener;

        public TeacherStateTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected List<OnlineTutoring> doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String classId = params[1];
            return mTutorService.queryTeacherState(accessToken, classId);
        }

        @Override
        protected void onResult(List<OnlineTutoring> onlineTutorings) {
            reqListener.onSuccess(onlineTutorings);
        }

        @Override
        protected void onFailure(HttpResponse<List<OnlineTutoring>> response, Exception ex) {
            reqListener.onFail(response, ex);
        }


    }

    /**
     * 获取辅导队列信息
     */
    class TutorQueueTask extends AppAsyncTask<String, Void, TutorQueue> {

        private RequestListener reqListener;

        public TutorQueueTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorQueue doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            return mTutorService.queryTutorQueue(accessToken, studentId);
        }

        @Override
        protected void onResult(TutorQueue tutorQueue) {
            reqListener.onSuccess(tutorQueue);
        }

        @Override
        protected void onFailure(HttpResponse<TutorQueue> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 申请辅导
     */
    class ApplyTutorTask extends AppAsyncTask<String, Void, ApplyTutor> {

        private RequestListener reqListener;

        public ApplyTutorTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected ApplyTutor doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            String teacherId = params[2];
            String imageIds = params[3];
            int estimateTime = Integer.parseInt(params[4]);
            return mTutorService.applyTutor(accessToken, studentId, teacherId, imageIds, estimateTime);
        }

        @Override
        protected void onResult(ApplyTutor applyTutor) {
            reqListener.onSuccess(applyTutor);
        }

        @Override
        protected void onFailure(HttpResponse<ApplyTutor> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 查询是否申请了辅导
     */
    class IsTutorAppliedTask extends AppAsyncTask<String, Void, TutorApplied> {

        private RequestListener reqListener;

        public IsTutorAppliedTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorApplied doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            return mTutorService.isTutorApplied(accessToken, studentId);
        }

        @Override
        protected void onResult(TutorApplied tutorApplied) {
            reqListener.onSuccess(tutorApplied);
        }

        @Override
        protected void onFailure(HttpResponse<TutorApplied> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 是否轮到辅导
     */
    class IsTutorTurnTask extends AppAsyncTask<String, Void, TutorTurn> {

        private RequestListener reqListener;

        public IsTutorTurnTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorTurn doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String studentId = params[1];
            String teacherId = params[2];
            return mTutorService.isTutorTurn(accessToken, studentId, teacherId);
        }

        @Override
        protected void onResult(TutorTurn tutorTurn) {
            reqListener.onSuccess(tutorTurn);
        }

        @Override
        protected void onFailure(HttpResponse<TutorTurn> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 修改辅导状态
     */
    class ModifyTutorStateTask extends AppAsyncTask<String, Void, TutorState> {

        private RequestListener reqListener;

        public ModifyTutorStateTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorState doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String coachId = params[1];
            int state = Integer.parseInt(params[2]);
            String remark = params[3];
            return mTutorService.modifyTutorState(accessToken, coachId, state, remark);
        }

        @Override
        protected void onResult(TutorState tutorState) {
            reqListener.onSuccess(tutorState);
        }

        @Override
        protected void onFailure(HttpResponse<TutorState> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 获取学生上传的图片
     */
    class UploadImagesTask extends AppAsyncTask<String, Void, List<UploadImage>> {

        private RequestListener reqListener;

        public UploadImagesTask(RequestListener requestListener) {
            reqListener = requestListener;
        }

        @Override
        protected List<UploadImage> doExecute(String... params) throws Exception {
            String accessToken = params[0];
            int pageNum = Integer.parseInt(params[1]);
            int pageSize = Integer.parseInt(params[2]);
            String studentId = params[3];
            return mTutorService.queryUploadImages(accessToken, pageNum, pageSize, studentId);
        }

        @Override
        protected void onResult(List<UploadImage> uploadImages) {
            reqListener.onSuccess(uploadImages);
        }

        @Override
        protected void onFailure(HttpResponse<List<UploadImage>> response, Exception ex) {
            reqListener.onFail(response, ex);
        }

    }

    /**
     * 进入辅导
     */
    class StartTutorTask extends AppAsyncTask<String, Void, TutorState> {

        private RequestListener reqListener;

        public StartTutorTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorState doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String coachId = params[1];
            String remark = params[2];
            return mTutorService.startTutor(accessToken, coachId,remark);
        }

        @Override
        protected void onResult(TutorState tutorState) {
            reqListener.onSuccess(tutorState);
        }

        @Override
        protected void onFailure(HttpResponse<TutorState> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 正常结束辅导
     */
    class EndTutorNormalTask extends AppAsyncTask<String, Void, TutorState> {

        private RequestListener reqListener;

        public EndTutorNormalTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorState doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String coachId = params[1];
            String remark = params[2];
            return mTutorService.endTutorNormal(accessToken, coachId,remark);
        }

        @Override
        protected void onResult(TutorState tutorState) {
            reqListener.onSuccess(tutorState);
        }

        @Override
        protected void onFailure(HttpResponse<TutorState> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    /**
     * 辅导异常结束
     */
    class EndTutorExceptionTask extends AppAsyncTask<String, Void, TutorState> {

        private RequestListener reqListener;

        public EndTutorExceptionTask(RequestListener requestListener) {
            reqListener = requestListener;
        }


        @Override
        protected TutorState doExecute(String... params) throws Exception {
            String accessToken = params[0];
            String coachId = params[1];
            String remark = params[2];
            return mTutorService.endTutorException(accessToken, coachId,remark);
        }

        @Override
        protected void onResult(TutorState tutorState) {
            reqListener.onSuccess(tutorState);
        }

        @Override
        protected void onFailure(HttpResponse<TutorState> response, Exception ex) {
            reqListener.onFail(response, ex);
        }
    }

    public void queryTeacherState(String access_token, String classId, RequestListener requestListener) {
        TeacherStateTask task = new TeacherStateTask(requestListener);
        task.execute(access_token, classId);
    }

    public void queryTutorQueue(String access_token, String studentId, RequestListener requestListener) {
        TutorQueueTask task = new TutorQueueTask(requestListener);
        task.execute(access_token, studentId);
    }

    public void applyTutor(String access_token, String studentId, String teacherId, String imageIds, int estimateTime, RequestListener requestListener) {
        ApplyTutorTask task = new ApplyTutorTask(requestListener);
        task.execute(access_token, studentId, teacherId, imageIds, estimateTime+"");
    }

    public void isTutorApplied(String access_token, String studentId, RequestListener requestListener) {
        IsTutorAppliedTask task = new IsTutorAppliedTask(requestListener);
        task.execute(access_token, studentId);
    }

    public void isTutorTurn(String access_token, String studentId, String teacherId, RequestListener requestListener) {
        IsTutorTurnTask task = new IsTutorTurnTask(requestListener);
        task.execute(access_token, studentId, teacherId);
    }

    public void modifyTutorState(String access_token, String coachId, int state, String remark, RequestListener requestListener) {
        ModifyTutorStateTask task = new ModifyTutorStateTask(requestListener);
        task.execute(access_token, coachId, state+"", remark);
    }

    public void queryUploadImages(String access_token, int pageNum, int pageSize, String studentId, RequestListener requestListener) {
        UploadImagesTask task = new UploadImagesTask(requestListener);
        task.execute(access_token, pageNum+"", pageSize+"", studentId);
    }

    public void startTutor(String access_token, int coachId,String remark, RequestListener requestListener) {
        StartTutorTask task = new StartTutorTask(requestListener);
        task.execute(access_token, coachId+"",remark);
    }

    public void endTutorNormal(String access_token, int coachId,String remark, RequestListener requestListener) {
        EndTutorNormalTask task = new EndTutorNormalTask(requestListener);
        task.execute(access_token, coachId+"",remark);
    }

    public void endTutorException(String access_token, int coachId,String remark, RequestListener requestListener) {
        EndTutorExceptionTask task = new EndTutorExceptionTask(requestListener);
        task.execute(access_token, coachId+"",remark);
    }

}
